var fundebug = require('./utils/fundebug.1.3.1.min.js');
fundebug.init(
  {
    apikey: '21da8acaa9949c254738343b15ec37923bc0aa85b15fc41c5467facf6d9f444d',
    httpTimeout: 6000,
    monitorHttpData: true,
    setSystemInfo: true,
    setUserInfo: true,
    monitorMethodCall: true,
    monitorMethodArguments: true
  })
// var fundebug = { notify:function(){}}

const inter = require('./utils/interface.js')
const webSocket = require("./utils/websocket.js")
App({
  onLaunch: function (options) {
    console.log(options)
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
    })
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
      wx.showModal({
        title: '更新提示',
        content: '新版本下载失败',
        showCancel: false
      })
    })
    this.iphoneX()
    // this.downLoadFamily()
  },
  onSocketMessageCallback: function (res) {
    let sockerRouter = JSON.parse(res)
    let that = this;
    switch (sockerRouter.data.type) {
      case "1": // 充值 
        wx.navigateTo({
          url: '/pages/paystatus/success/success?data=' + JSON.stringify(sockerRouter.data),
        })
        break;
      case "2": // 消费加油
        wx.navigateTo({
          url: '/pages/paystatus/success/success?data=' + JSON.stringify(sockerRouter.data),
        })
        break;
    }
  },
  copyCityId(){
    let cityId = wx.getStorageSync("cityId")
    if (cityId) {
      wx.setStorageSync("cityIdFZ", cityId)
    }
  },
  onHide(){
    wx.removeStorageSync("cityId")
  },
  onShow(){
    let cityId = wx.getStorageSync("cityIdFZ")
    if (cityId) {
      wx.setStorageSync("cityId", cityId)
      wx.removeStorageSync("cityIdFZ")
    }
  },
  login(){
    let that = this
    return new Promise((result,rej)=>{
      let openid = wx.getStorageSync("openid")
      if(openid){
        wx.checkSession({
          success() {
            result()
          },
          fail() {
            that.wxLogin(result)
          }
        })
      }else{
        that.wxLogin(result)
      }
    })
  },
  wxLogin(result){
    wx.login({
      success: res => {
        inter.wxOpenid({ code: res.code }).then(res => {
          if (res.data.code == 200) {
            wx.setStorageSync('openid', res.data.data)
            inter.getMemberInfoByOpenId({ openid: res.data.data}).then(res=>{
              wx.setStorageSync('userInfo', res.data.data)
              result(res.data)
            })
          }
        })
      }
    })
  },
  iphoneX(){
    let _self = this
    wx.getSystemInfo({
      success: res => {
        let modelmes = res.model;
        if (modelmes.search('iPhone X') != -1 || modelmes.search('iPhone 11') != -1 || modelmes.search('iPhone 11 Pro Max') != -1) {
          _self.globalData.isIphoneX = true
        }
        wx.setStorageSync('modelmes', modelmes)
      }
    })
  },
  downLoadFamily(){
    wx.loadFontFace({
      family: 'PingFang Bold',
      source: 'url("https://www.careoe.com:9443/h5/font/PingFang Bold.ttf")',
      success: function () {
        console.log('下载 Bold')
      }
    })
    wx.loadFontFace({
      family: 'PingFang Medium',
      source: 'url("https://www.careoe.com:9443/h5/font/PingFang Medium.ttf")',
      success: function(){
        console.log('下载 Medium')
      }
    })
  },
  globalData: {
    isApp: null,
    url: "https://www.careoe.com:9443/rn/#/",
    // https://www.careoe.com:9443/h5/font/
    // url: "http://192.168.1.108:3000/#/",
    isIphoneX:false,
    flag:false,
    fundebug: fundebug
  }
})